var IS_CURRENT_DOCUMENT_SUBMITTED = false ;
var CHECK = 700;

function currencyFormat(num)
{	
	num = num + "";
	if(num.indexOf('.') == -1) 
		noDecPointLength = num.length;
	else
		noDecPointLength = num.substring(0,num.indexOf('.')).length;

	beginPoint = noDecPointLength%3 != 0 ? noDecPointLength%3 : 3;
	
	for( ;beginPoint <noDecPointLength; beginPoint+=4, noDecPointLength++){
		num = num.substring(0,beginPoint) + "," + num.substring(beginPoint);	
	}
	return num;
}

function globalReplace(oriString, replaceFromString, replaceToString){
	if(oriString !="" && oriString != "undefined" && oriString !=null && oriString !="NaN")
		return oriString.replace(eval("/" + replaceFromString  + "/g") , "");
	return oriString;
}

function toFixedDecimal(amount, decimalPlace)
{
	var result = amount + "";
	var pointIndex = result.indexOf(".");
	if(isNaN(amount) || amount == "Infinity" || amount == "-Infinity") return 0.00;
		
	if(pointIndex >= 0)
	{
		//append zero
		for(appendIndexx=0; appendIndexx<decimalPlace; appendIndexx++)
			result = result + "0";
			
		if(result.charAt(pointIndex + decimalPlace + 1)>=5)
			addOne =  eval(result.charAt(pointIndex + decimalPlace)) + 1;
		else
			addOne = result.charAt(pointIndex + decimalPlace);
		result = result.substring(0, pointIndex + decimalPlace) + addOne;
		
		//remove last char if last char is "."
		if(result.charAt(result.length-1) ==".")
			result = result.substring(0, result.length-1) ;
	}
	else if(pointIndex < 0 && decimalPlace == 0)
		return amount;
	else
		result = result + ".00";
		
	return result;
}

function isNumeric(sText){
	var validChars = "0123456789.";
 	var isNumber=true;
   	var pointCount = 0;
   	var charr;
	
	sText = globalReplace(sText, ",", "");
	
	if(sText=="") return false;

   	for (stringIndex = 0; stringIndex < sText.length && isNumber == true; stringIndex++) 
   	{ 
      charr = sText.charAt(stringIndex); 
      if (validChars.indexOf(charr) == -1) 
         isNumber = false;
      if(charr == ".")
      	   pointCount++;
      if(pointCount>2) return false;
    }
    return isNumber;
}

function clearAll(form){	
	for (  i=0 ; i < form.elements.length ; i++ ) {		
		typee = form.elements[i].type;	
		//alert(typee);
		if ( typee == "text" || typee == "password")
			form.elements[i].value = "";
		else if(typee=="radio" || typee == "checkbox" )
			form.elements[i].checked = false;
		else if(typee=="select-one")
			form.elements[i].value = "";
	}
}

function verifyDouble(textField,decimalPlace){
	
	var textValue = textField.value;	
	
	var theLen = textValue.length; 
	
	var dotPos = textValue.indexOf("."); 	
	
	/*This is to cater to no dot value.
	*/
	if (dotPos < 0) {
		dotPos = theLen-1;
	}	

	var commaPos = textValue.lastIndexOf(",");
	
	if(commaPos>dotPos)
		return false;
	
	textValue=textValue.replace(/,/gi,"");
	textField.value=textValue;	
	
	var thePos = theLen - (dotPos+1);
	var isNum = (isNaN(textValue)) ? false:true; 	
	var isAt = (thePos <= decimalPlace) ? true:false; 	
	var isOne = (textValue.indexOf(".",dotPos+1) == -1) ? true:false; 	
	
	if((isNum) && (isAt) && (isOne)) { 
		return true;	
	} else { 
		return false;	
	} 
}

function parseDec(val,places,sep) {

	// This function takes two arguments:
	//   (string || number)  val
	//            (integer)  places
	//             (string)  sep
	// val is the numeric string or number to parse
	// places represents the number of decimal
	// places to return at the end of the parse.
	// sep is an optional string to be used to separate
	// the whole units from the decimal units (default: '.')

	val = '' + val;
		// Implicitly cast val to (string)
	
	if (!sep) {
		sep = '.';
		// If separator isn't specified, then use a decimal point '.'
	}
	
	if (!places) { places = 0; }
	places = parseInt(places);
		// Make sure places is an integer
	
//	if (!parseInt(val)) {
//		// If val is null, zero, NaN, or not specified, then
//		// assume val to be zero.  Add 'places' number of zeros after
//		// the separator 'sep', and then return the value.  We're done here.
//		val = '0';
//		if (places > 0) {
//			val += sep;
//			while (val.substring((val.indexOf(sep))).length <= places) {
//				val += '0';
//			}
//		}
//		return val;
//	}
	
	if ((val.indexOf('.') > -1) && (sep != '.')) {
		val = val.substring(0,val.indexOf('.')) + sep + val.substring(val.indexOf('.')+1);
			// If we're using a separator other than '.' then convert now.
	}
		
	if (val.indexOf(sep) > -1) {
		// If our val has a separator, then cut our value
		// into pre and post 'decimal' based upon the separator.
		pre = val.substring(0,val.indexOf(sep));
		post = val.substring(val.indexOf(sep)+1);
	} else {
		// Otherwise pre gets everything and post gets nothing.
		pre = val;
		post = '';
	}
	
	if (places > 0) {

		// If we're dealing with a decimal then...
		post = post.substring(0,(places+1));
			// We care most about the digit after 'places'
		
		if (post.length > places) {
			// If we have trailing decimal places then...

			if ( parseInt(post.substring(post.length - 1)) > 4 ) {
				post = '' + Math.round(parseInt(post) / 10);
				//post = '' + post.substring(0,post.length - 2) + (1/Math.pow(10,places));
				//post = ('' + post.substring(0,post.length - 2)) + (parseInt(post.substring(post.length - 1)) + 1);
			} else {
				post = '' + Math.round(parseInt(post));
			}
		}
		
		if (post.length > places) {

			post = '' + Math.round(parseInt(post.substring(0,places)));
		} else if (post.length < places) {
			while (post.length < places) {
				post += '0';
			}
		}
	
	} else {

		if (parseInt((post.substring(0,1))) > 4) {
			pre = '' + (parseInt(pre) + 1);
		} else {
			pre = '' + (parseInt(pre));	
		}
		post = '';
	}
	
	sep = (post.length > 0) ? sep : '';
		// Should we use a separator?
	val = pre + sep + post;
		// Rebuild val

	return val;
	
}

function roundOff(value){
	var floatValue = parseFloat(value);
   	var roundOffValue = (Math.round(floatValue*100))/100;
   	return roundOffValue;
}

function roundOffByDecimal(value,decimal) {
	var floatValue = parseFloat(value);
	if (decimal == 2) {
	   	var roundOffValue = (Math.round(floatValue*100))/100;
	   	return roundOffValue;	
	} else if (decimal == 3) {
		var roundOffValue = (Math.round(floatValue*1000))/1000;
	   	return roundOffValue;	
	} else {
	   	var roundOffValue = (Math.round(floatValue*100))/100;
	   	return roundOffValue;	
	}
}

function checkAll(form,checkAllObject) {
	var chkAll = checkAllObject.checked;
	
	for (  i=0 ; i < form.elements.length ; i++ ) {			
		if ( form.elements[i].type == "checkbox" ){
			form.elements[i].checked = chkAll;
		}
	}
}

function checkAllNotDisabled(form,checkAllObject) {
	var chkAll = checkAllObject.checked;
	
	for (  i=0 ; i < form.elements.length ; i++ ) {			
		if ( form.elements[i].type == "checkbox" ){
			if(form.elements[i].disabled != true)
				form.elements[i].checked = chkAll;
		}
	}
} 

function checkAllById(formName,tableName,checkAllObject,checkBoxName,lastRowId) {
	var chkAll = checkAllObject.checked;
	
	for (  i=0 ; i < 10 ; i++ ) {			
		cboxName=formName+':'+tableName+'_'+i+':'+checkBoxName;
		checkbox=document.getElementById(cboxName);
		if ( checkbox != null ){
			checkbox.checked = chkAll;
		}
	}
}		

function checkAllSubTableById(formName,tableName,subTableName,checkAllObject,checkBoxName,lastRowId) {
	var chkAll = checkAllObject.checked;
	
	for (  i=0 ; i < 10 ; i++ ) {			
		for (  j=0 ; j < 10 ; j++ ){
			cboxName=formName+':'+tableName+'_'+i+':'+subTableName+'_'+j+':'+checkBoxName;
			checkbox=document.getElementById(cboxName);
			if ( checkbox != null ){
				checkbox.checked = chkAll;
			}
		}
	}
}

function goPage(url)
{
	document.location.href=url;
}

function toggleSection(id)
{ 
	var section = document.getElementById(id); 
    if(section.style.display == "none")
    {
        section.style.display = "block";
    }
}

function hideSection(id)
{
	var section = document.getElementById(id); 
    if(section.style.display == "block")
    {
        section.style.display = "none";
    }
}

/************************************************************
Move selected items from one list box to another

Phil Webb (phil@philwebb.com)
http://www.philwebb.com
*************************************************************/
function move(fbox, tbox) {

var arrFbox = new Array();
var arrTbox = new Array();
var arrLookup = new Array();
var i;
for (i = 0; i < tbox.options.length; i++) {
	arrLookup[tbox.options[i].text] = tbox.options[i].value;
	arrTbox[i] = tbox.options[i].text;
}
var fLength = 0;
var tLength = arrTbox.length;
for(i = 0; i < fbox.options.length; i++) {
	arrLookup[fbox.options[i].text] = fbox.options[i].value;
	if (fbox.options[i].selected && fbox.options[i].value != "") {
		arrTbox[tLength] = fbox.options[i].text;
		tLength++;
	}
	else {
		arrFbox[fLength] = fbox.options[i].text;
		fLength++;
	}
}
arrFbox.sort();
arrTbox.sort();
fbox.length = 0;
tbox.length = 0;
var c;
for(c = 0; c < arrFbox.length; c++) {
var no = new Option();
no.value = arrLookup[arrFbox[c]];
no.text = arrFbox[c];
fbox[c] = no;
}
for(c = 0; c < arrTbox.length; c++) {
var no = new Option();
no.value = arrLookup[arrTbox[c]];
no.text = arrTbox[c];
tbox[c] = no;
   }
}


/************************************************************************
 * this function will limit the user to enter only number of decimal that*
 * been set earlier 														*
 ************************************************************************/
function validateTotalDecimalValue(tempValue,decimalNo,location){
	
	var valueThis = tempValue.value; 
	var tempNumberOfDecimals = 0;
	var foundDecimal = false;
	var tempNo ="";
	var tempReturnVal="";
	
 	if(valueThis.charAt(0)!=0){
	 	for(x=0;x<valueThis.length;x++){
		 		if(valueThis.charAt(x)=='.' && foundDecimal==false){
		 			 foundDecimal=true;
		 		}
		 		
		 		if(foundDecimal==true){
		 				tempNumberOfDecimals++;
		 		}
		 		
		 		if(tempNumberOfDecimals<=decimalNo+1){
		 			tempReturnVal+=valueThis.charAt(x)
		 		}
	 	}
 		document.getElementById(location).value = tempReturnVal;
 	}
 }


function checkDoubleSubmission() {

	if (!IS_CURRENT_DOCUMENT_SUBMITTED) {
		IS_CURRENT_DOCUMENT_SUBMITTED=true;		
		return false;
	} else {
		return true;	
	}

}

function removeComma(id) {
		var tempEstimatedPrice = document.getElementById(id).value;
		var tempCharAt;
		var tempResult =""; 
		
		for (x=0; x<tempEstimatedPrice.length; x++){
			tempCharAt = tempEstimatedPrice.charAt(x);
			
				if(tempCharAt != ","){
					tempResult += tempCharAt;
				}
		}
		
		document.getElementById(id).value = tempResult;
		return tempResult;
}

function removeCommaValue(val) {
		var tempEstimatedPrice = val;
		var tempCharAt;
		var tempResult =""; 
		
		for (x=0; x<tempEstimatedPrice.length; x++){
			tempCharAt = tempEstimatedPrice.charAt(x);
			
				if(tempCharAt != ","){
					tempResult += tempCharAt;
				}
		}
		
		return tempResult;
}

function verifyCheckBox(form,msg,isEmpty,msg2) {
	var countChkBox = 0;
	for (i=0; i<form.elements.length; i++) {
		var typeName = form.elements[i].type;
		var elementName = form.elements[i].name;
		if (isEmpty == 'true') {	
			alert(msg2);
			return false;
		} else {
			if (typeName == "checkbox" && (elementName.indexOf("chkSingle") > -1 ) ) {
				var element = document.getElementById(elementName);
				if (element.checked) {
					++countChkBox;
				}
			}
		}
	}
	if (countChkBox > 0) {
		return true;
	} else {
		alert(msg);
		return false;
	}
}

function formatCurrency(strValue,decimalPoint) {
	strTotal = strValue.substring(0,strValue.indexOf("."));
	strDecimal = strValue.substring(strValue.indexOf(".")+1,strValue.length); 
	var index = strTotal.length;
	var constructPrice = '';
	if (decimalPoint == strDecimal.length || decimalPoint < strDecimal.length) {
		//do nothing
	} else {
		decimalPoint = decimalPoint - strDecimal.length;
		for(x=0;x<decimalPoint;x++) {
			strDecimal = strDecimal+"0";
		}
	}

	if (index>3) {
		while (index>0) {
			if (index == strTotal.length) {
				index=index-3
				constructPrice = ","+strTotal.substr(index,3);
			} else {
				if (index>3) {
					index=index-3;
					temp = ","+strTotal.substr(index,3);
					constructPrice = temp+constructPrice;
				} else { 
					constructPrice = strTotal.substring(0,index)+constructPrice;
					break;
				}
			}
		}
		return constructPrice+"."+strDecimal;
	} else {
		return strValue;
	}
}

function convertText(value) {
	  value = value.toUpperCase();
	  return value;
 }